 /**
 * 逻辑、功能相关描述: <br>
 *
 * @author 付湘辉  <br>
 * 编写日期：2008-12-15  <br>
 */
package com.ce.pms.custservice.dao.impl;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.util.Assert;

import com.ce.pms.base.dao.impl.PmsBaseDaoImpl;
import com.ce.pms.common.model.HmDeptDomain;
import com.ce.pms.custservice.dao.ISubmitTaskDao;
import com.ce.pms.custservice.model.SubmitTaskDO;
import com.ce.pms.custservice.vo.SubmitTaskSearchVO;
import com.ce.util.BlankUtil;
import com.sitechasia.webx.core.support.Page;

/**
 * 	名称: com.ce.pms.custservice.dao.impl.SubmitTaskDaoImpl <br>
 *
 *
 * @author  付湘辉  <br>
 * Created on： 2008-12-15 <br>
 * @author <br>
 * update on： <br>
 * why: <br>
 */
public class SubmitTaskDaoImpl extends PmsBaseDaoImpl<SubmitTaskDO> implements ISubmitTaskDao {
	/**
	 * 方法描述: 分页查询所有任务
	 * @author 胡煦
	 * Create on:2008-12-19
	 * @param SearchVO
	 * @return 返回当前页的查询结果
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public Page queryAllSubmitTaskDOByPage(SubmitTaskSearchVO searchVO){
		Map<String,Object> parameterMap = new HashMap<String,Object>();
		StringBuilder queryStr =  new StringBuilder("FROM SubmitTaskDO st where 1=1 and st.submitType = 1 ");
		queryStr.append(this.generateWhereAndOrderSentence(searchVO, parameterMap));
		return super.pagedNameQueryByPageNo(queryStr.toString(), searchVO.getPageNo(), searchVO.getPageSize(),parameterMap);
	}
	/**
	    * 功能描述:生成查询所有任务分页列表的条件和排序子句  <br>
	    * @param searchVO,Map
	    * @return String
	    * Create author:胡煦 <br>
	    * Create on:2008-12-19  <br>
	    * Edit author:  <br>
	    * Edit on:    <br>
	    * Why:     <br>
	    */
	   private String generateWhereAndOrderSentence(SubmitTaskSearchVO searchVO,Map<String,Object> parameterMap) {
		   StringBuilder sb = new StringBuilder();
		   // 部门ID
			if(!BlankUtil.isBlank(searchVO.getDeptId())){
				sb.append(" and st.deptDO.id = :deptId");
				parameterMap.put("deptId", searchVO.getDeptId());
			}
			//客服事件ID
			if(!BlankUtil.isBlank(searchVO.getCustServiceEventId())){
				sb.append(" and st.custServiceEventDO.id = :custServiceEventId");
				parameterMap.put("custServiceEventId", searchVO.getCustServiceEventId());
			}
			//事件性质ID
			if(!BlankUtil.isBlank(searchVO.getCustServEventNatureId())){
				sb.append(" and st.custServiceEventDO.customerServiceEventNatureDO.id = :custServEventNatureId");
				parameterMap.put("custServEventNatureId", searchVO.getCustServEventNatureId());
			}
			//状态
			if(!BlankUtil.isBlank(searchVO.getStatuz())){
				sb.append(" and st.statuz = :statuz");
				parameterMap.put("statuz", searchVO.getStatuz());
			}
			//责任部门Name
			if(!BlankUtil.isBlank(searchVO.getDeptName())){
				sb.append(" and st.deptDO.deptName like :deptName");
				parameterMap.put("deptName", "%"+searchVO.getDeptName()+"%");
			}
			//房号Name
			if(!BlankUtil.isBlank(searchVO.getHouseNumber())){
				sb.append(" and st.custServiceEventDO.houseNumber like :houseNumber");
				parameterMap.put("houseNumber", "%"+searchVO.getHouseNumber()+"%");
			}
			if(!BlankUtil.isBlank(searchVO.getOrderKey())&&!BlankUtil.isBlank(searchVO.getOrderValue())){
				sb.append(" order by st.").append(searchVO.getOrderValue()).append(" ").
				append(searchVO.getOrderKey());

			}else{
				sb.append(" order by st.sysInfo.sysDate");
			}

			return sb.toString();
	   }


	public List<SubmitTaskDO> querySubmitTaskListByEventID(Long eventID,String submitType)
	{
		Assert.notNull(eventID);
		StringBuilder hqlBuilder = new StringBuilder();
		hqlBuilder.append("SELECT OBJ ").append(" FROM ").append(SubmitTaskDO.class.getSimpleName())
		.append(" AS OBJ WHERE ").append("OBJ.custServiceEventDO.keyword = ? AND OBJ.validity = ? ");
		if(!BlankUtil.isBlank(submitType)){
			hqlBuilder.append(" AND OBJ.submitType = ?");
			return findAllResult(hqlBuilder.toString(), eventID, SubmitTaskDO.TASK_VALIDITY_VALID_CODE,submitType);
		}else{
			return findAllResult(hqlBuilder.toString(), eventID, SubmitTaskDO.TASK_VALIDITY_VALID_CODE);
		}
	}

	/**
	 * 功能描述: 查询事件关联的(手动创建的)任务所对应的部门列表 <br>
	 * @param eventID
	 * @return
	 * Create author: 王浩羽 <br>
	 * Create on:Jan 14, 2009  <br>
	 * Edit author:  <br>
	 * Edit on:    <br>
	 * Why:     <br>
	 */
	public List<HmDeptDomain> queryDepartmentsWithValidSubmitTask(Long eventID){
		Assert.notNull(eventID);
		StringBuilder hqlBuilder = new StringBuilder();
		hqlBuilder.append("SELECT OBJ.deptDO ").append(" FROM ").append(SubmitTaskDO.class.getSimpleName())
		.append(" AS OBJ WHERE ").append("OBJ.custServiceEventDO.keyword = ? AND OBJ.validity = ? AND OBJ.submitType = ? ");
		return findAllResult(hqlBuilder.toString(), eventID, SubmitTaskDO.TASK_VALIDITY_VALID_CODE, SubmitTaskDO.TASK_SUBMIT_TYPE_MANUAL);
	}

	/**
	 * 方法描述: 批量提交任务
	 * @author 胡煦
	 * Create on:2008-9-9
	 * @param List<Object[]> list
	 * @return boolean
	 * Edit author:
	 * Edit on:
	 * 修改原因：
	 */
	public boolean batchInsertSubmitTasks(List<Object[]> list){
		String sql = "insert into CE_DISPATCH_EVENT ( CUST_SERVICE_EVENT_ID,DEPT_ID,DEPT_PRINCIPAL,STATUZ,FREQUENCY,CREATOR,SYSDATE,VALIDITY,AUTO_SUBMIT ) values( ?,?,?,'001',0,?,now(),1,1)";
		this.executeSQLBatch(sql, list);
		return true;
	}
}
